import Layout from '@/views/layout'
import Layout2 from '@/views/layout/index2'
import component from '@/views/basicsManage/index'
import { getSession } from '@/utils'

const fzslaughterData = {
  path: '/slaughterData',
  name: 'slaughterDataManagement',
  component: Layout,
  meta: { title: '杏花鸡屠宰数据', noCache: true, breadcrumb: false  },
  children: [{
    path: 'index',
    name: 'slaughterData',
    component: () => import('@/views/basicsManage/index'),
    meta: { title: '杏花鸡屠宰数据', noCache: true }
  }]
}

const harmlessData = {
  path: '/harmlessData',
  name: 'harmlessDataManagement',
  component: Layout,
  meta: { title: '无害化处理数据', noCache: true, breadcrumb: false  },
  children: [{
    path: 'index',
    name: 'harmlessData',
    component: () => import('@/views/basicsManage/index'),
    meta: { title: '无害化处理数据', noCache: true }
  }]
}

const similarSaleData = {
  path: '/similarSale',
  name: 'similarSaleManagement',
  component: Layout,
  meta: { title: '同类产品交易地区分析', noCache: true, breadcrumb: false  },
  children: [{
    path: 'index',
    name: 'similarSale',
    component: () => import('@/views/basicsManage/index'),
    meta: { title: '同类产品交易地区分析', noCache: true }
  }]
}
const deadChickenData = {
  path: '/deadChicken',
  name: 'deadChickenManagement',
  component: Layout,
  meta: { title: '病死鸡数量分布', noCache: true, breadcrumb: false  },
  children: [{
    path: 'index',
    name: 'deadChicken',
    component: () => import('@/views/basicsManage/index'),
    meta: { title: '病死鸡数量分布', noCache: true }
  }]
}
const poultryDisDistributionData = {
  path: '/poultryDisDistribution',
  name: 'poultryDisDistributionManagement',
  component: Layout,
  meta: { title: '禽疾病分布', noCache: true, breadcrumb: false  },
  children: [{
    path: 'index',
    name: 'poultryDisDistribution',
    component: () => import('@/views/basicsManage/index'),
    meta: { title: '禽疾病分布', noCache: true }
  }]
}
const insuranceTypeData = {
  path: '/insuranceType',
  name: 'insuranceTypeManagement',
  component: Layout,
  meta: { title: '保险类型', noCache: true, breadcrumb: false  },
  children: [{
    path: 'index',
    name: 'insuranceType',
    component: () => import('@/views/basicsManage/index'),
    meta: { title: '保险类型', noCache: true }
  }]
}

const brandData = {
  path: '/brand',
  name: 'brandManagement',
  component: Layout,
  meta: { title: '杏花鸡品牌', noCache: true, breadcrumb: false  },
  children: [{
    path: 'index',
    name: 'brand',
    component: () => import('@/views/basicsManage/index'),
    meta: { title: '杏花鸡品牌', noCache: true }
  }]
}

function addDataRoute(routes, roles){
  let res=[], roleChild = []
  const findItem = roles.find(el => el.menuUrl === 'dataManagement');
  if (findItem) {
    roles.map(el => {
      if(findItem.id === el.parentId) roleChild.push(el)
    })
    roleChild.forEach((role) => {
      let child = []
      // 抚州项目，添加无害化处理/屠宰企业数据，判断是否有权限
      let menuPermission = getSession('menuPermission');
      if(role.menuUrl === 'importAndExportAnalysis') {
        let item = menuPermission.find(el => el.menuUrl === 'similarSale');
        if (item) {
          similarSaleData.children[0].meta.rights = item.rights
          child.push(similarSaleData);
        }
      }
      if(role.menuUrl === 'harmlessTreatmentTwo') {
        let item = menuPermission.find(el => el.menuUrl === 'deadChicken');
        if (item) {
          deadChickenData.children[0].meta.rights = item.rights
          child.push(deadChickenData);
        }
      }
      if(role.menuUrl === 'bg_investment') {
        let item = menuPermission.find(el => el.menuUrl === 'poultryDisDistribution');
        if (item) {
          poultryDisDistributionData.children[0].meta.rights = item.rights
          child.push(poultryDisDistributionData);
        }
      }
      if(role.menuUrl === 'financial_three') {
        let item = menuPermission.find(el => el.menuUrl === 'insuranceType');
        if (item) {
          insuranceTypeData.children[0].meta.rights = item.rights
          child.push(insuranceTypeData);
        }
      }
      if(role.projectId === 2 && role.menuUrl === 'bg_harmlessTreatmentAnalysis') {
        let item = menuPermission.find(el => el.menuUrl === 'harmlessData');
        if (item) child.push(harmlessData);
      }
      if(role.projectId === 2 && role.menuUrl === 'bg_pigSlaughter') {
        let item = menuPermission.find(el => el.menuUrl === 'slaughterData');
        if (item) child.push(fzslaughterData);
      }

      routes.forEach((route) => {
        if (route.status===0 && route.parentId === role.id) {
          const obj = {
            path: route.menuUrl,
            name: role.menuPermission,
            component: Layout,
            meta: { title: route.menuName, breadcrumb: false },
            redirect: `${route.menuUrl}/index`,
            needAuth: false,
            children: [{
              path: 'index',
              name: route.menuPermission+'index',
              component: component,
              meta: { title: route.menuName, menuId: route.menuId, rights: route.rights }
            }]
          }
          child.push(obj)
        }
      })
      
      if(role.menuUrl === 'bg_industrial') {
        let item = menuPermission.find(el => el.menuUrl === 'brand');
        if (item) {
          brandData.children[0].meta.rights = item.rights
          child.push(brandData);
        }
      }

      const tmp={
        path: role.menuUrl,
        name: role.menuUrl,
        component: Layout2,
        meta: { title: role.menuName, breadcrumb: false },
        needAuth: false,
        children: child
      }
      child.length>0 && res.push(tmp)
    })
  }
  return res
}

export default addDataRoute
